﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
using DaiChong.Lib.Http;
using System.Threading;
using WTest.Model;

namespace WTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region 新闻
        private void btnNews_Click(object sender, EventArgs e)
        {
            List<News> lst = new List<News>();

            //1.首页列表
            Submit web = new Submit
            {
                Encode = Encoding.GetEncoding("gb2312"),
                URL = "http://www.narutom.com/news/"
            };
            web.Get();
            ParseNewsList(web.Html, ref lst);

            int pages = 0;
            Match mch = Regex.Match(web.Html, @"分页：页次：<b>1/([^<]+?)</b>&nbsp;每页<b>([^<]+?)</b>&nbsp;总数<b>([^<]+?)</b>");
            if (mch.Success)
                pages = Convert.ToInt32(mch.Groups[1].Value);
            pages = pages > 10 ? 10 : pages;

            //2.其他页列表
            for (int i = 1; i < pages; i++)
            {
                web = new Submit
                {
                    Encode = Encoding.GetEncoding("gb2312"),
                    URL = string.Format("http://www.narutom.com/news/index_{0}.html", i + 1)
                };
                web.Get();
                ParseNewsList(web.Html, ref lst);
            }
            
            //3.采新闻内容
            int len = lst.Count;
            string contents = "";
            for (int i = 0; i < len; i++)
            {
                web = new Submit
                {
                    Encode = Encoding.GetEncoding("gb2312"),
                    URL = lst[i].TempUrl
                };
                web.Get();

                //发布时间 发布时间：2012-07-07
                mch = Regex.Match(web.Html, @"发布时间：([^\r]+?)\r");
                if (mch.Success)
                    lst[i].News_AddTime = GetTime(mch.Groups[1].Value);

                //内容
                mch = Regex.Match(web.Html, @"<div class=""rt4"" id=""newsbody"">([\s\S]+?)<div class=""epages""></div>");
                if (mch.Success)
                {
                    contents = mch.Groups[1].Value;
                    contents = contents.Replace("'", "").Replace("\"","").Replace("火影忍者中文网（www.narutom.com）", "5917动漫网（www.5917le.com）");
                    lst[i].News_Content = contents;
                }
            }

            for (int i = 0; i < len; i++)
            {
                News.Insert(lst[i]);
            }
            lblStatus.Text = "ok";
        }

        private void ParseNewsList(string html, ref List<News> lst)
        {
            Match mch = Regex.Match(html, @"<dl><dt><a href=""([^""]+?)"" title=""([^""]+?)"".*?<dd>([^<]+?)</dd></dl>");
            while (mch.Success)
            {
                News n = new News()
                {
                    News_CId = 23,
                    News_Name = mch.Groups[2].Value.Replace("'", "").Replace("\"", ""),
                    News_Remark = mch.Groups[3].Value.Replace("'", "").Replace("\"", ""),
                    News_Content = "",
                    News_AddTime = 1341736771,
                    TempUrl = mch.Groups[1].Value
                };
                lst.Add(n);
                mch = mch.NextMatch();
            }
        }
        #endregion

        #region 漫画

        private void btnMh_Click(object sender, EventArgs e)
        {
            List<News> lst = new List<News>();

            //1.首页列表
            Submit web = new Submit
            {
                Encode = Encoding.GetEncoding("gb2312"),
                URL = "http://www.narutom.com/comic/"
            };
            web.Get();
            ParseDmList(web.Html, ref lst);

            int pages = 0;
            Match mch = Regex.Match(web.Html, @"分页：页次：<b>1/([^<]+?)</b>&nbsp;每页<b>([^<]+?)</b>&nbsp;总数<b>([^<]+?)</b>");
            if (mch.Success)
                pages = Convert.ToInt32(mch.Groups[1].Value);
            pages = pages > 20 ? 20 : pages;

            //2.其他页列表
            for (int i = 1; i < pages; i++)
            {
                web = new Submit
                {
                    Encode = Encoding.GetEncoding("gb2312"),
                    URL = string.Format("http://www.narutom.com/comic/index_{0}.html", i + 1)
                };
                web.Get();
                ParseDmList(web.Html, ref lst);
            }

            //3.采新闻内容
            int len = lst.Count;
            string contents = "";
            for (int i = 0; i < len; i++)
            {
                web = new Submit
                {
                    Encode = Encoding.GetEncoding("gb2312"),
                    URL = lst[i].TempUrl
                };
                web.Get();

                //发布时间 发布时间：2012-07-07
                mch = Regex.Match(web.Html, @"发布时间：([^\r]+?)\r");
                if (mch.Success)
                    lst[i].News_AddTime = GetTime(mch.Groups[1].Value);

                //内容
                mch = Regex.Match(web.Html, @"<div class=""rt4"" id=""newsbody"">([\s\S]+?)<div class=""epages""></div>");
                if (mch.Success)
                {
                    contents = mch.Groups[1].Value;
                    contents = contents.Replace("'", "").Replace("\"", "").Replace("火影忍者中文网（www.narutom.com）", "5917动漫网（www.5917dm.com）");
                    lst[i].News_Content = contents;
                }
            }

            for (int i = 0; i < len; i++)
            {
                News.Insert(lst[i]);
            }
            lblStatus.Text = "ok";
        }

        private void ParseDmList(string html, ref List<News> lst)
        {
            string str = "";
            Match mch = Regex.Match(html, @"<div id=""dm_name"">([\s\S]+?)</div>");
            if (mch.Success)
            {
                str = mch.Groups[1].Value;
                mch = Regex.Match(str, @"<li><a href=""([^""]+?)"" target=""_blank"" title=""([^""]+?)"">");
                while (mch.Success)
                {
                    News n = new News()
                    {
                        News_CId = 23,
                        News_Name = mch.Groups[2].Value.Replace("'", "").Replace("\"", ""),
                        News_Remark = mch.Groups[2].Value.Replace("'", "").Replace("\"", ""),
                        News_Content = "",
                        News_AddTime = 1341736771,
                        TempUrl = mch.Groups[1].Value
                    };
                    lst.Add(n);
                    mch = mch.NextMatch();
                }
            }
        }

        #endregion

        private long GetTime(string dateStr)
        {
            DateTime dt = Convert.ToDateTime(dateStr);
            return (dt.Ticks - DateTime.Parse("1970-01-01 00:00:00").Ticks) / 10000000;
        }
    }
}
